package groovyuno

import com.sun.star.beans.XPropertySet;
import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.frame.XDesktop;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

class UNO {
	private static XMultiComponentFactory mcf
	private static XComponentContext defaultContext
	static XDesktop desktop;

	public static void init() {
		def remoteServiceManager = Bootstrap.bootstrap().getServiceManager()

		mcf = (XMultiComponentFactory)UnoRuntime.queryInterface(XMultiComponentFactory.class, remoteServiceManager)
		defaultContext = (XComponentContext)UnoRuntime.queryInterface(XComponentContext.class,
				((XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, mcf)).
				getPropertyValue("DefaultContext"))
		desktop = UnoRuntime.queryInterface(XDesktop.class, mcf.createInstanceWithContext("com.sun.star.frame.Desktop", defaultContext));
		
		UNOExpander.expand();
	}
	
	public static Object createUNOService(String serviceName) {
		try{
			return mcf.createInstanceWithContext(serviceName, defaultContext)
		}catch(Exception x) {
			return null;
		}
	}
}
